<?php

class CFilter {

	public function __construct(){

	}

	
	static public function var_filter(&$thisArray){
		//global $thisArray;
		if (is_array($thisArray)) {
			foreach ($thisArray as $key => $value) {
				if (!is_array($value)) {
					$thisArray[$key] = stripslashes($value);
				} 
				else {
					CFilter::var_filter($thisArray[$key]);
				}
			}
		}
		else{
			$thisArray=stripslashes($thisArray);
		}
		//return $thisArray;
	}
	
	static public function getFilterVar($name){
		$_GET[$name]=mysql_escape_string($_GET[$name]);
		return $_GET[$name];
	}
	
	static public function postFilterVar($name){
		$_POST[$name]=mysql_escape_string($_POST[$name]);
		return $_POST[$name];
	}
	
	static public function postFilterArray($thisPost){
		if (is_array($thisPost)) {
			foreach ($thisPost as $key => $value) {
				if (!is_array($value)) {
					$thisPost[$key] = mysql_escape_string($value);
				} 
				else {
					CFilter::postFilterArray($thisPost[$key]);
				}
			}
			return $thisPost;
		}
		else{
			return "参数错误！必须为数组类型！";
		}
	}
	
	static public function filterArrayEncode($thisPost){
		if (is_array($thisPost)) {
			foreach ($thisPost as $key => $value) {
				if (!is_array($value)) {
					$thisPost[$key] = CFilter::htmlEncode(trim($value));
				} 
				else {
					CFilter::filterArrayEncode($thisPost[$key]);
				}
			}
			return $thisPost;
		}
		else{
			return "参数错误！必须为数组类型！";
		}
	}
	
	
	static public function htmlEncode($msg){
		return str_replace(array('&amp;','&nbsp;','"',"'",'<','>',"\t","\r",'  '),array('&',' ','&quot;','&#39;','&lt;','&gt;','&nbsp; &nbsp;','','&nbsp;'),$msg);
	}
	
	
	static public function SpHtml2Text($str){
  		$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
  		$alltext = "";
  		$start = 1;
  		for($i=0;$i<strlen($str);$i++){
    		if($start==0 && $str[$i]==">") $start = 1;
    		else if($start==1){
     			if($str[$i]=="<"){ $start = 0; $alltext .= " "; }
     			else if(ord($str[$i])>31) $alltext .= $str[$i];
    		}
 		 }
  		$alltext = str_replace("　"," ",$alltext);
 		$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
 		$alltext = preg_replace("/[ ]+/s"," ",$alltext);
  		return $alltext;
	}
	
	static public function filterScript($str){
		$str=preg_replace("/<script[^>]*?>.*?<\\/script>/","",$str);
		return $str;
	}
	
	static public function filterSpecialChars($string,$replacement=""){
		$string=trim($string);
		$string=preg_replace("/[^a-zA-Z\x80-\xff\s0-9]/",$replacement,$string);
		return $string;
	}
	
	static public function filterFileName($string,$replacement){
		$thisArray=explode(".",$string);
		$fileType=$thisArray[count($thisArray)-1];
		$string=trim($string);
		$fileName=str_replace($fileType,"",$string);
		$fileName=preg_replace("/[^a-zA-Z0-9]/",$replacement,$fileName);
		return $fileName.".".$fileType;
	}
	
	static public function filterPage($page,$counts,$perPage){
		if($counts==0){
			return array('page'=>1,'pageNumber'=>0,'from'=>0);
		}
		$pageNumber=intval(($counts-1)/$perPage)+1;
		if($page<1||$page==""){
			$page=1;
		}
		else if($page>$pageNumber){
			$page=$pageNumber;
		}
		$from=($page-1)*$perPage;
		return array('page'=>$page,'pageNumber'=>$pageNumber,'from'=>$from);
		
	}
	
	static public function filterFile($thisFile){
		foreach($thisFile as $key => $value){
			foreach($value as $key2 => $value2){
				foreach($value2 as $key3 => $value3){
					if($value3==""){
						unset($thisFile[$key][$key2][$key3]);
						
					}
				}
			}
		}
		return $thisFile;
	}
	
	static public function trimPost(&$thisPost,$expArray=array()){
		foreach($thisPost as $key => $value){
			if(is_array($thisPost[$key])){
				self::trimPost($thisPost[$key],$expArray);
			}
			else if(!in_array($key,$expArray)){
				$thisPost[$key]=trim($thisPost[$key]);
			}
		}
		return $thisPost;
	}
	
}
